package ringbuffer;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.WorkHandler;
import com.lmax.disruptor.dsl.Disruptor;

public class Main
{
    public static void main(String[] args) throws Exception
    {
        // Executor that will be used to construct new threads for consumers
        Executor executor = Executors.newCachedThreadPool();
        
//        ThreadFactory factory = 

        // Specify the size of the ring buffer, must be power of 2.
        int bufferSize = 1024;

        // Construct the Disruptor
        Disruptor<MsgEvent> disruptor = new Disruptor<MsgEvent>(new MsgEventFactory(), bufferSize, executor);

        // Connect the handler
        EventHandler<MsgEvent> handler_a = new MsgEventConsumer();
        EventHandler<MsgEvent> handler_b = new MsgEventConsumer();
//        disruptor.handleEventsWith(handler_a).then(handler_b);
        disruptor.handleEventsWith(handler_a,handler_b);
        
//        WorkHandler<MsgEvent> workHandler_a = new MsgEventConsumer_WorkHandler();
//        WorkHandler<MsgEvent> workHandler_b = new MsgEventConsumer_WorkHandler();
//        disruptor.handleEventsWithWorkerPool(workHandler_a,workHandler_b);
        // Start the Disruptor, starts all threads running
        disruptor.start();

        // Get the ring buffer from the Disruptor to be used for publishing.
        RingBuffer<MsgEvent> ringBuffer = disruptor.getRingBuffer();
        MsgEventProducerWithTranslator producer = new MsgEventProducerWithTranslator(ringBuffer);
        for (long l = 0; true; l++)
        {
            producer.onData( "liangz_"+l);
            Thread.sleep(1000);
        }
    }
}